Skip to content

feat: improve tag support for parity with formatjs#24

Merged
ramsey merged 1 commit intomainfrom
feature/improve-tag-support
Dec 14, 2021
Merged

feat: improve tag support for parity with formatjs#24
ramsey merged 1 commit intomainfrom
feature/improve-tag-support

Conversation

@ramsey
Copy link
Copy Markdown
Contributor

@ramsey ramsey commented Dec 13, 2021

Description

It turns out that FormatJS doesn't parse real HTML/XML tags in messages. Instead, they use custom tags that can be associated with a callback to replace the tag with a computed value.

For example:

$formatphp->formatMessage([
    'id' => 'myMessage',
    'defaultMessage' => 'Hello, <profileLink>{name}</profileLink>!',
], [
    'name' => 'Ben',
    'profileLink' => fn ($text) => '<a href="https://example.com/user/1234">' . $text . '</a>',
]);

When rendered, this will produce:

Hello, <a href="https://example.com/user/1234">Ben</a>!

See: https://formatjs.io/docs/intl#defaultrichtextelements

In the Config object, we're also able to pass in a "global" array of callbacks as the defaultRichTextElements parameter. Any named callbacks configured this way will apply to all formatted messages.

For example:

$locale = new Locale('en-US');
$config = new Config($locale, null, [
    'homeLink' => fn (string $text): string => '<a href="https://example.com>' . $text . '</a>',
    'boldface' => fn ($text) => "<strong>$text</strong>",
    'italicized' => fn ($text) => "<em>$text</em>",
]);

$message = new Message('myMessage', '<homeLink>Go <boldface>home</boldface></homeLink>, {name}!');
$messageCollection = new MessageCollection([$message]);
$formatphp = new FormatPHP($config, $messageCollection);

echo $formatphp->formatMessage([
    'id' => 'myMessage',
    'defaultMessage' => '<homeLink>Go <boldface>home</boldface></homeLink>, {name}!',
], [
    'name' => 'Samwise',
]);

This will produce:

<a href="https://example.com>Go <strong>home</strong></a>, Samwise!

Product requirements and context

To support pseudo-locale generation, we have implemented a parser that is identical to that of FormatJS. This means our parser can't properly handle attributes and all the inside guts of an HTML tag. As a result, we can't pass <a> tags with HREFs. So, we need to implement the same tag functionality as FormatJS.

I think this is the last requirement before I can finally finish pseudo locales.

How has this been tested?

PR Checklist

  • I have added tests to cover my changes.

@ramsey ramsey requested a review from a team December 13, 2021 23:10
Comment thread src/Intl/MessageFormat.php
@qlty-cloud-legacy
Copy link
Copy Markdown

Code Climate has analyzed commit edf14dc and detected 1 issue on this pull request.

Here's the issue category breakdown:

Category Count
Complexity 1

The test coverage on the diff in this pull request is 100.0% (80% is the threshold).

This pull request will bring the total coverage in the repository to 96.4% (0.1% change).

View more on Code Climate.

@ramsey ramsey requested review from jrode, ldiego08 and maccath December 13, 2021 23:15
Copy link
Copy Markdown

@jrode jrode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice tests.

@ramsey ramsey merged commit a36bde1 into main Dec 14, 2021
@ramsey ramsey deleted the feature/improve-tag-support branch December 14, 2021 16:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants